package me.aartikov.screenresultsample.screens;


import me.aartikov.alligator.AbilityResult;
import me.aartikov.alligator.converters.OneWayIntentConverter;
import me.aartikov.alligator.converters.OneWayScreenResultConverter;
import me.aartikov.alligator.Screen;
import me.aartikov.alligator.ScreenResult;
import ohos.aafwk.content.Intent;
import ohos.app.Context;
import ohos.utils.net.Uri;

public class ImagePickerScreen extends Screen {

    public ImagePickerScreen() {
        super(0, null);
    }

    // Screen result
    public static class Result implements ScreenResult {
        private Uri mUri;

        public Result(Uri uri) {
            mUri = uri;
        }

        public Uri getUri() {
            return mUri;
        }
    }

    // Intent converter. Creates an intent from a screen.
    public static class Converter extends OneWayIntentConverter<ImagePickerScreen> {

        @Override
        public Intent createIntent(Context context, ImagePickerScreen screen) {
            Intent intent = new Intent();
            intent.setAction("android.intent.action.PICK");
            intent.addFlags(Intent.FLAG_NOT_OHOS_COMPONENT);
            intent.setType("image/*");
            return intent;
        }
    }

    // Screen result converter. Creates a screen result from ActivityResult.
    public static class ResultConverter extends OneWayScreenResultConverter<Result> {

        @Override
        public ImagePickerScreen.Result getScreenResult(AbilityResult activityResult) {
            Uri uri = activityResult.getDataUri();
            return uri != null ? new Result(uri) : null;
        }
    }
}
